package com.cf.docker;

import com.cf.docker.controller.ContainerController;
import com.cf.docker.controller.ImageController;
import com.cf.docker.controller.MachineController;
import com.cf.docker.service.*;
import com.cf.docker.websocket.WebShellSocket;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.freemarker.FreeMarkerProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * boot-awm-DockerConfig
 *
 * @author <a href="2428922347@qq.com">Zhu mingming</a>
 * @info
 * @since 2018/6/20 下午 03:03
 */
@Slf4j
@Configuration
public class DockerConfig implements InitializingBean {


    //#################controller####################
    @Bean
    @ConditionalOnMissingBean(ContainerController.class)
    public ContainerController containerController() {
        log.info("实例化默认ContainerController");
        return new ContainerController();
    }

    @Bean
    @ConditionalOnMissingBean(ImageController.class)
    public ImageController imageController() {
        log.info("实例化默认ImageController");
        return new ImageController();
    }

    @Bean
    @ConditionalOnMissingBean(MachineController.class)
    public MachineController machineController() {
        log.info("实例化默认MachineController");
        return new MachineController();
    }

    //################# service ####################

    @Bean
    @ConditionalOnMissingBean(ContainerService.class)
    public ContainerService containerService() {
        log.info("实例化默认ContainerService");
        return new ContainerService();
    }

    @Bean
    @ConditionalOnMissingBean(ImageService.class)
    public ImageService imageService() {
        log.info("实例化默认ImageService");
        return new ImageService();
    }

    @Bean
    @ConditionalOnMissingBean(MachineService.class)
    public MachineService machineService() {
        log.info("实例化默认MachineService");
        return new MachineService();
    }

    @Bean
    @ConditionalOnMissingBean(SSHService.class)
    public SSHService sshService() {
        log.info("实例化默认SSHService");
        return new SSHService();
    }

    @Bean
    @ConditionalOnMissingBean(DockerService.class)
    public DockerService dockerService() {
        log.info("实例化默认DockerService");
        return new DockerService();
    }

    @Bean
    @ConditionalOnMissingBean(WebShellSocket.class)
    public WebShellSocket webShellSocket() {
        log.info("实例化默认WebShellSocket");
        return new WebShellSocket();
    }



    @Autowired
    private FreeMarkerProperties freeMarkerProperties;

    @Override
    public void afterPropertiesSet() throws Exception {
        String[] templateLoaderPath = freeMarkerProperties.getTemplateLoaderPath();
        ArrayList<String> paths = new ArrayList();
        paths.addAll(Arrays.asList(templateLoaderPath));
        paths.add("classpath:/template-docker/");
        String[] strings = paths.toArray(new String[paths.size()]);
        freeMarkerProperties.setTemplateLoaderPath(strings);
    }


}
